<?php
namespace App\Controller;

use Think\Cache;

class OrderController extends ApiController
{
      //会员地址信息显示
      public function order_list(){
          if(IS_POST) {
              $user_id=I('post.user_id');
              if (empty($user_id)||!is_numeric($user_id)) {
                  json_Response('-200','请先登录','null',2);
              }
              $count=D('User')->where("id={$user_id}")->count();
              if(!$count){
                  json_Response('-200','请先登录','null',2);
              }
              $p=I('post.p');
              $pagesize=I('post.pagesize');
              $flag=I('post.flag');
              if(empty($p)||empty($pagesize)||empty($flag)){
                  json_Response(-200, '参数错误', [], 2);
              }
              if(!is_numeric($p)||!is_numeric($pagesize)||!is_numeric($flag)){
                  json_Response(-200, '参数错误', [], 2);
              }
              if($p<=0){
                  $p=1;
              }
              if($flag==1){//全部

              }elseif ($flag==2){//未付款
                  $arr['price_status'] = 0;
              }elseif ($flag==3){//已付款，待发货
                  $arr['price_status'] = 1;
                  $arr['indent_status'] = 0;
              }elseif ($flag==4){//已发货
                  $arr['price_status'] = 1;
                  $arr['indent_status'] = 1;
              }elseif ($flag==5){//完成
                  $arr['price_status'] = 1;
                  $arr['indent_status'] = 2;
              }
              $arr['user_id'] = $user_id;
              $data= M('Gongying_indent')
                  ->where($arr)
                  ->page($p,$pagesize)
                  ->select();
              foreach ($data as $k=>&$v){
                  $v['picture_small']=C('api_address').$v['picture_small'];
              }

              if (empty($data)) {
                  json_Response(-200, '传输失败', $data=[], 1);
              } else {
                  json_Response(200, '传输成功', $data, 1);
              }
      }else{
              json_Response('-200','传输失败','null',2);
          }
      }
      //订单详情
      public function order_detail(){
          if(IS_POST) {
              $user_id=I('post.user_id');
              if (empty($user_id)||!is_numeric($user_id)) {
                  json_Response('-200','请先登录','null',2);
              }
              $count=D('User')->where("id={$user_id}")->count();
              if(!$count){
                  json_Response('-200','请先登录','null',2);
              }

              $id = I('post.order_id');
              $supplier_id = I('post.supplier_id');
              if (empty($id)||!is_numeric($id)||$supplier_id==""||!is_numeric($supplier_id)) {
                  json_Response('-200','参数错误','null',2);
              }

              if($supplier_id){
                  $data = M('Gongying_indent')->alias('a')
                      ->join('inner join tpshop_goods as b on a.goods_name = b.goods_name')
                      ->where("a.id = {$id}")->find();
              }else{
                  $data = M('Gongying_indent')->alias('a')
                      ->join('inner join tpshop_seller_selfgoods as b on a.goods_name = b.goods_name')
                      ->where("a.id = {$id}")->find();
              }

                  $data['picture_small']=C('api_address').$data['picture_small'];

              //查询快递信息
              $orderData=D('GongyingIndent')->alias('a')
                  ->field('a.price_status,a.indent_status,a.seller_id,a.consignee,a.tell,a.city_rel,a.city_town,a.city_classify,a.shipping_address,a.order_number,a.expressNo,b.com,b.expressName')
                  ->join('LEFT JOIN __EXPRESS__ b ON a.expressId=b.expressId')
                  ->where(array(
                      'a.id'=>array('eq',$id),
                      'a.user_id'=>array('eq',$user_id)
                  ))
                  ->select();

              /*快递实时查询开始*/
              $com=$orderData[0]['com'];
              $num=$orderData[0]['expressno'];
              $res=express($com,$num);
              //解析返回结果
              $return='';
              if($res == false){
                  //请求失败
                  //$this->error("物流跟踪请求失败");
                  $return=1;
              }
              $res_arr = json_decode($res, true);
              if($res_arr['status'] != 200){
                  //查询失败';
                  // $this->error("物流跟踪查询失败");
                  $return=1;
              }
              if($return==1){
                  $data['express']='物流跟踪失败，请确定已发货，并且物流信息正确';
              }else{
                  $data['express']=$res_arr['data'];
              }
              json_Response(200,'传输成功',$data,1);

          }else{
              json_Response('-200','传输失败','null',2);
          }
      }
}
